package com.leexuan.ordersystem.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.leexuan.ordersystem.beans.Invoice;
import com.leexuan.ordersystem.beans.Order;
import com.leexuan.ordersystem.db.InvoiceDB;
import com.leexuan.ordersystem.db.MoneyDB;
import com.leexuan.ordersystem.db.OrderDB;

public class ListMoneyDetailAction extends Action {

	public ListMoneyDetailAction() {
		super();
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		int oid = Integer.parseInt(request.getParameter("oid"));

		HttpSession session = request.getSession();

		OrderDB odb = new OrderDB();

		InvoiceDB idb = new InvoiceDB();

		MoneyDB mdb = new MoneyDB();

		try {
			idb.getConnection();

			Invoice invoice = idb.getInvoice(oid);

			odb.getConnection();

			Order order = odb.getOrder(oid);

			mdb.getConnection();

			ArrayList moneys = null;

			moneys = mdb.getMoneys(invoice.getIid());

			session.setAttribute("order", order);
			session.setAttribute("invoice", invoice);
			session.setAttribute("moneys", moneys);

			return mapping.findForward("success");

		} catch (Exception e) {

			return (new ActionForward(mapping.getInput()));
		} finally {
			try {
				odb.closeConnection();
				idb.closeConnection();
				mdb.closeConnection();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}
